/**
 * 客户端测试用例编组模型
 *
 * Created by liuxuechao on 2022/08/12
 */

const request = require('request-promise');
const querystring = require('querystring');
const testerURI = require('./../api/tester/uri');

class TestCaseGroup {
  constructor(func, functionId, groupId, kind = undefined) {
    this.func = func;
    this.functionId = functionId;
    this.groupId = groupId;
    this.kind = kind;
  }

  /**
   * 获取编组
   * @return {Promise}
   */
  getTeseCaseGroupChildren() {
    return new Promise((resolve, reject) => {
      let group;
      this.func.rootDirectory.projectVersion.project.creator.login()
        .then(() => {
          const query = querystring.stringify({ groupId: this.groupId });
          const { accessToken } = this.func.rootDirectory.projectVersion.project.creator.token;
          const url = `${testerURI}/functions/${this.functionId}/testcase-groups.json?${query}`;
          const options = {
            url,
            headers: { Authorization: `Bearer ${accessToken}` },
            json: true,
          };
          return request.get(options);
        })
        .then((res) => {
          group = res.children;
          return this.func.rootDirectory.projectVersion.project.creator.logout();
          // resolve(group);
        })
        .then(() => resolve(group))
        .catch(reject);
    });
  }
}

/**
 * Export model definition object.
 */
module.exports = TestCaseGroup;
